package com.flipdf2.serialize.ppml.element;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;
import org.jdom.Namespace;

import com.flipdf2.serialize.ppml.PPMLException;

public class PPMLPage {
	public static final String NAME = "PAGE";
	
	private List<PPMLMetadata> metadataList;

	private PPMLSuppliedResources suppliedResources;
	private PPMLRequiredResources requiredResources;
	
	private PPMLPageDesign pageDesign;
	
	private List<PPMLPrivateInfo> privateInfoList;
	
	private List<PPMLTicketSet> ticketSetList;
	private List<PPMLTicketRef> ticketRefList;
	private List<PPMLReusableObject> reusableObjectList;
	private List<PPMLSegmentArray> segmentArrayList;
	private List<PPMLMark> markList;
	
	private String datasetClass;
	private String label;
    private double[][] dimensions;
	
	public Element toXML(Namespace defaultNamespace) {
		Element root = null;
		if (defaultNamespace != null) {
			root = new Element(NAME, defaultNamespace);
		} else {
			root = new Element(NAME);	
		}
		
		if (metadataList != null && metadataList.size() > 0) {
			for (int i = 0, n = metadataList.size(); i < n; i++) {
				root.addContent(metadataList.get(i).toXML(defaultNamespace));
			}
		}
		
		if (suppliedResources != null)
			root.addContent(suppliedResources.toXML(defaultNamespace));
		
		if (requiredResources != null)
			root.addContent(requiredResources.toXML(defaultNamespace));
		
		if (pageDesign != null)
			root.addContent(pageDesign.toXML(defaultNamespace));
		
		if (privateInfoList != null && privateInfoList.size() > 0) {
			for (int i = 0, n = privateInfoList.size(); i < n; i++) {
				root.addContent(privateInfoList.get(i).toXML(defaultNamespace));
			}
		}
		
		if (ticketSetList != null && ticketSetList.size() > 0) {
			for (int i = 0, n = ticketSetList.size(); i < n; i++) {
				root.addContent(ticketSetList.get(i).toXML(defaultNamespace));
			}
		}

		if (ticketRefList != null && ticketRefList.size() > 0) {
			for (int i = 0, n = ticketRefList.size(); i < n; i++) {
				root.addContent(ticketRefList.get(i).toXML(defaultNamespace));
			}
		}

		if (reusableObjectList != null && reusableObjectList.size() > 0) {
			for (int i = 0, n = reusableObjectList.size(); i < n; i++) {
				root.addContent(reusableObjectList.get(i).toXML(defaultNamespace));
			}
		}

		if (segmentArrayList != null && segmentArrayList.size() > 0) {
			for (int i = 0, n = segmentArrayList.size(); i < n; i++) {
				root.addContent(segmentArrayList.get(i).toXML(defaultNamespace));
			}
		}

		if (markList != null && markList.size() > 0) {
			for (int i = 0, n = markList.size(); i < n; i++) {
				root.addContent(markList.get(i).toXML(defaultNamespace));
			}
		}
		
		if (datasetClass != null) 
			root.setAttribute("Class", datasetClass);
		if (label != null) 
			root.setAttribute("Label", label);
		if (dimensions != null) 
			root.setAttribute("Dimensions", dimensions[0] + " " + dimensions[1]);
		return root;
	}
	
	public void check() throws PPMLException {
	}

	public String getDatasetClass() {
		return datasetClass;
	}

	public void setDatasetClass(String datasetClass) {
		this.datasetClass = datasetClass;
	}

	public double[][] getDimensions() {
		return dimensions;
	}

	public void setDimensions(double[][] dimensions) {
		this.dimensions = dimensions;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public List<PPMLMark> getMarkList() {
		return markList;
	}

	public void setMarkList(List<PPMLMark> markList) {
		this.markList = markList;
	}
	
	public void addMark(PPMLMark mark) {
		if (this.markList == null)
			this.markList = new ArrayList<PPMLMark>();
		this.markList.add(mark);
	}
	
	public void addMark(int position, PPMLMark mark) {
		if (this.markList == null)
			this.markList = new ArrayList<PPMLMark>();
		this.markList.add(position, mark);
	}

	public List<PPMLMetadata> getMetadataList() {
		return metadataList;
	}

	public void setMetadataList(List<PPMLMetadata> metadataList) {
		this.metadataList = metadataList;
	}
	
	public void addMetadata(PPMLMetadata metadata) {
		if (this.metadataList == null)
			this.metadataList = new ArrayList<PPMLMetadata>();
		this.metadataList.add(metadata);
	}

	public PPMLPageDesign getPageDesign() {
		return pageDesign;
	}

	public void setPageDesign(PPMLPageDesign pageDesign) {
		this.pageDesign = pageDesign;
	}

	public List<PPMLPrivateInfo> getPrivateInfoList() {
		return privateInfoList;
	}

	public void setPrivateInfoList(List<PPMLPrivateInfo> privateInfoList) {
		this.privateInfoList = privateInfoList;
	}
	
	public void addPrivateInfo(PPMLPrivateInfo privateInfo) {
		if (this.privateInfoList == null)
			this.privateInfoList = new ArrayList<PPMLPrivateInfo>();
		this.privateInfoList.add(privateInfo);
	}

	public PPMLRequiredResources getRequiredResources() {
		return requiredResources;
	}

	public void setRequiredResources(PPMLRequiredResources requiredResources) {
		this.requiredResources = requiredResources;
	}

	public List<PPMLReusableObject> getReusableObjectList() {
		return reusableObjectList;
	}

	public void setReusableObjectList(List<PPMLReusableObject> reusableObjectList) {
		this.reusableObjectList = reusableObjectList;
	}

	public void addReusableObject(PPMLReusableObject reusableObject) {
		if (this.reusableObjectList == null)
			this.reusableObjectList = new ArrayList<PPMLReusableObject>();
		this.reusableObjectList.add(reusableObject);
	}

	public List<PPMLSegmentArray> getSegmentArrayList() {
		return segmentArrayList;
	}

	public void setSegmentArrayList(List<PPMLSegmentArray> segmentArrayList) {
		this.segmentArrayList = segmentArrayList;
	}
	
	public void addSegmentArray(PPMLSegmentArray segmentArray) {
		if (this.segmentArrayList == null)
			this.segmentArrayList = new ArrayList<PPMLSegmentArray>();
		this.segmentArrayList.add(segmentArray);
	}

	public PPMLSuppliedResources getSuppliedResources() {
		return suppliedResources;
	}

	public void setSuppliedResources(PPMLSuppliedResources suppliedResources) {
		this.suppliedResources = suppliedResources;
	}

	public List<PPMLTicketRef> getTicketRefList() {
		return ticketRefList;
	}

	public void setTicketRefList(List<PPMLTicketRef> ticketRefList) {
		this.ticketRefList = ticketRefList;
	}
	
	public void addTicketRef(PPMLTicketRef ticketRef) {
		if (this.ticketRefList == null)
			this.ticketRefList = new ArrayList<PPMLTicketRef>();
		this.ticketRefList.add(ticketRef);
	}

	public List<PPMLTicketSet> getTicketSetList() {
		return ticketSetList;
	}

	public void setTicketSetList(List<PPMLTicketSet> ticketSetList) {
		this.ticketSetList = ticketSetList;
	}
	
	public void addTicketSet(PPMLTicketSet ticketSet) {
		if (this.ticketSetList == null)
			this.ticketSetList = new ArrayList<PPMLTicketSet>();
		this.ticketSetList.add(ticketSet);
	}
}
